#include "fresnel_effect.h"
#include "graphics/cube_texture_gl.h"
#include "geometry/point.h"

namespace venus {
FresnelEffect::FresnelEffect(float eta_ratio, float fresnel_bias, 
                             float fresnel_scale, float fresnel_power)
    : eta_ratio_(eta_ratio), fresnel_bias_(fresnel_bias),
      fresnel_scale_(fresnel_scale), fresnel_power_(fresnel_power) {
  program_.Load("shaders/fresnel.vp", "shaders/fresnel.fp");
  program_.Link();
  eye_pos_var_ = program_.GetUniformVar("eye_pos");
  //material_color_var_ = program_.GetUniformVar("material_color");
  eta_ratio_var_ = program_.GetUniformVar("eta_ratio");
  fresnel_bias_var_ = program_.GetUniformVar("fresnel_bias");
  fresnel_scale_var_ = program_.GetUniformVar("fresnel_scale");
  fresnel_power_var_ = program_.GetUniformVar("fresnel_power");
  env_map_var_ = program_.GetUniformVar("env_map");
}

void FresnelEffect::Begin() const {
  program_.Begin();
  glUniform3fv(eye_pos_var_, 1, eye_pos_->ptr());
  //glUniform4fv(material_color_var_, material_color_->ptr());
  glUniform1f(eta_ratio_var_, eta_ratio_);
  glUniform1f(fresnel_bias_var_, fresnel_bias_);
  glUniform1f(fresnel_scale_var_, fresnel_scale_);
  glUniform1f(fresnel_power_var_, fresnel_power_);
  glUniform1i(env_map_var_, env_map_);
}
void FresnelEffect::End() const {
  program_.End();
}
} // namespace venus
